from bs4 import BeautifulSoup
import csv, os
os.chdir("D:/KIPRIS/Family")

# csv file and csv object
f = open("Family.csv", "w", encoding="utf-8", newline="")
wf = csv.writer(f)

for year in range(1984, 2018):
	directory = "D:/KIPRIS/Family/Family{}.xml".format(year)
	file = open(directory, "r", encoding= "utf-8")
	txt = file.read()
	txt = txt.replace("\n", " ")
	xml_list = txt.split('<?xml version="1.0" encoding="utf-8"?>')
	for xml in xml_list[1:]:
		family = BeautifulSoup(xml, 'xml')
		anum = family.find("appnum").text
		docs = family.find_all("patentFamilyInfo")
		for i, doc in enumerate(docs):
			order = i + 1
			ccode = doc.find("countryCode").text
			country = doc.find("countryName").text
			fam_kind = doc.find("familyKind").text
			fam_numb = doc.find("literatureNumber").text
			lit_kind = doc.find("literatureKind").text
			lit_num = doc.find("literatureNumber").text
			open_num = doc.find("openingNumber").text
			line = [anum, order, ccode, country, fam_kind, fam_numb, lit_kind, lit_num, open_num]
			wf.writerow(line)
			print(line)